import { createStore } from 'vuex'
import axios from 'axios'
export default createStore({
  state: {
    messageList: []
  },
  mutations: {
    setMessageList(state, payload) {
      state.messageList = payload
    }
  },
  actions: {
    async fetchMessageList({ commit }) {
      const resp = await axios.get('/api/message/lists');
      const { code, data } = resp.data;
      if (code === 200) {
        commit('setMessageList', data)
      }
    },
    async addMessageList({ dispatch }, payload) {
      const resp = await axios.post('/api/message/list', payload)

      const { code } = resp.data
      if (code === 200) {
        dispatch('fetchMessageList')
      }

      return resp
    }
  },
  modules: {
  }
})
